package com.ukao.steward.ui.login;


import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.VerifysBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.listener.MyTextWatcher;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.AppManager;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.CipherUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 设置密码
 */
public class SetNewPassFrament extends BaseFragment {


    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.msg)
    TextView msg;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.signin_btn)
    Button signinBtn;
    private VerifysBean.DataBean mVerifysBean;
    private String mParam2;
    private Unbinder unbinder;


    public static SetNewPassFrament newInstance(Parcelable param1, String param2) {
        SetNewPassFrament fragment = new SetNewPassFrament();
        Bundle args = new Bundle();
        args.putParcelable(Constant.ARG_PARAM1, param1);
        args.putString(Constant.ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {

            mVerifysBean = getArguments().getParcelable(Constant.ARG_PARAM1);
            mParam2 = getArguments().getString(Constant.ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_set_new_pass, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();

    }

    @Override
    protected void initView() {
        setStatusBarTintColor(R.color.normal_blue);
        if (!CheckUtils.isNull(mVerifysBean)) {
            msg.setText(mVerifysBean.getMsg());
            title.setText("设置新密码");
        }
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));


    }

    @Override
    protected void initListener() {
        super.initListener();
        code.addTextChangedListener(useNameTextWatcher);
        password.addTextChangedListener(passTextWatcher);

    }


    private MyTextWatcher useNameTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            isILoginGo(s, password);
        }
    };
    private MyTextWatcher passTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            isILoginGo(s, code);
        }

    };

    private void isILoginGo(CharSequence s, EditText mEditText) {
        String strPass = s.toString().trim();
        String strUseName = getText(mEditText);
        if (strPass.isEmpty() || strUseName.isEmpty()) {
            signinBtn.setAlpha(0.5f);
            signinBtn.setEnabled(false);
        } else {
            signinBtn.setAlpha(1f);
            signinBtn.setEnabled(true);
        }
    }

    /**
     * 获取签名
     */
    public void resetPassword(String code, String password) {
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("account", mVerifysBean.getAccount());
        mHashMap.put("code", code);
        mHashMap.put("password", CipherUtils.md5(password));
        L.i("account=" + mVerifysBean.getAccount() + "strpassword=" + password);
        addSubscription(apiStores().resetPassword(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            AppManager.getAppManager().finishActivity(VerifyLoginFrament.class);
                            finish();

                        }
                        T.show(model.getMsg());
                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();

                    }
                });
    }


    @OnClick({R.id.back_btn, R.id.signin_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.signin_btn:
                String strphone = getText(code);
                String strpassword = getText(password);
                if (CheckUtils.isEmpty(strphone)) {
                    T.show("请验证码");
                    return;
                } else if (CheckUtils.isEmpty(strpassword)) {
                    T.show("请输入密码");
                    return;
                }
                if (!CheckUtils.isNull(mVerifysBean))
                    resetPassword(strphone, strpassword);
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
